package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.dto.CommissionDTO;
import com.qiangqiang.task.server.dto.CommissionRecordDTO;
import com.qiangqiang.task.server.entity.Commission;
import com.qiangqiang.task.server.mapper.CommissionMapper;
import com.qiangqiang.task.server.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-03-10 16:50
 **/
@Service
public class CommissionService extends BaseService {

    @Autowired
    private CommissionMapper commissionMapper;

    @Autowired
    private UserMapper userMapper;

    public CommissionDTO findAllCommissionByUserId(long userId) {
        CommissionDTO commissionDTO = new CommissionDTO();
        commissionDTO.setTotalCommission(userMapper.queryUserById(userId).getCommission());

        List<Commission> commissionList = commissionMapper.queryAllCommissionByUserId(userId);
        List<CommissionRecordDTO> recordList = new ArrayList<>();
        for (Commission item : commissionList) {
            CommissionRecordDTO commissionRecordDTO = new CommissionRecordDTO();
            commissionRecordDTO.setTitle(item.getTaskTitle());
            commissionRecordDTO.setReward(item.getReward());
            recordList.add(commissionRecordDTO);
        }
        commissionDTO.setRecordList(recordList);
        return commissionDTO;

    }
}
